package com.lcx.plugin.neteaseCloud.music.player;

import cn.hutool.core.util.StrUtil;
import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * Java 播放Mp3音频，支持暂停和继续播放
 *
 * @author daxiong
 * @date 2021/10/14 下午2:32
 */
@Deprecated
public class Mp3Player {

    private String url;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public boolean isNewBegin() {
        return Objects.isNull(clip);
    }


    private int point;
    private Clip clip;

    public static void main(String[] args) throws Exception {
        Mp3Player player = new Mp3Player();
        player.setUrl("http://m7.music.126.net/20211015104311/ceee467d55b09d8184a3b53fc4f8c6c0/ymusic/f9af/f007/b4f5/665536c971402e77fd52b802a6cc75ec.mp3");
//        player.playMp3File("/Users/daxiong/Downloads/11.mp3");
        player.play();
        TimeUnit.SECONDS.sleep(5);
        System.out.println("暂停3s");
        player.pause();
        TimeUnit.SECONDS.sleep(3);
        System.out.println("继续播放");
        player.play();
        TimeUnit.SECONDS.sleep(20);
    }

    /**
     * 初始化为未播放状态
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/10/14 下午2:30
     */
    public void init() {
        this.point = 0;
        this.clip = null;
    }

    /**
     * 播放mp3
     *
     * @return void
     * @author daxiong
     * @date 2021/10/14 下午3:37
     */
    public void playMp3Url() throws UnsupportedAudioFileException, IOException {
        if (StrUtil.isNotEmpty(getUrl())) {
            playMp3Url(getUrl());
        }
    }

    /**
     * Java Music 播放 mp3
     *
     * @param url mp3文件的url
     * @throws IOException
     * @throws UnsupportedAudioFileException
     * @Title: play_mp3
     * @Description: 播放 mp3
     * @date 2019年10月25日 下午12:28:41
     */
    public void playMp3Url(String url) throws UnsupportedAudioFileException, IOException {
        // 使用 mp3spi 解码 mp3 音频文件
        MpegAudioFileReader mp = new MpegAudioFileReader();
        AudioInputStream stream = mp.getAudioInputStream(new URL(url));
        playMp3File(stream);
    }

    /**
     * Java Music 播放 mp3
     *
     * @param path mp3文件路径
     * @throws IOException
     * @throws UnsupportedAudioFileException
     * @Title: play_mp3
     * @Description: 播放 mp3
     * @date 2019年10月25日 下午12:28:41
     */
    public void playMp3File(String path) throws UnsupportedAudioFileException, IOException {
        File file = new File(path);
        if (!file.exists() || !path.toLowerCase().endsWith(".mp3")) {
            throw new RuntimeException("文件不存在");
        }
        // 使用 mp3spi 解码 mp3 音频文件
        MpegAudioFileReader mp = new MpegAudioFileReader();
        AudioInputStream stream = mp.getAudioInputStream(file);
        playMp3File(stream);
    }

    /**
     * 播放mp3 流文件
     *
     * @param stream
     * @return void
     * @author daxiong
     * @date 2021/10/14 下午3:02
     */
    private void playMp3File(AudioInputStream stream) throws IOException {
        AudioFormat baseFormat = stream.getFormat();
        // 设定输出格式为pcm格式的音频文件
//        AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(),
//                16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
        AudioFormat format = new AudioFormat(baseFormat.getSampleRate(),
                16, baseFormat.getChannels(), true, false);
        // 输出到音频
        stream = AudioSystem.getAudioInputStream(format, stream);
        AudioFormat target = stream.getFormat();
        DataLine.Info dinfo = new DataLine.Info(Clip.class, target, AudioSystem.NOT_SPECIFIED);
        try {
            clip = (Clip) AudioSystem.getLine(dinfo);
            clip.open(stream);
            stream.close();
            clip.start();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            stream.close();
        }
    }


    public void pause() {
        if (Objects.nonNull(clip)) {
            // 获取当前播放中继点
            point = clip.getFramePosition();
            // 停止播放
            clip.stop();
        }
    }

    public void play() {
        if (isNewBegin()) {
            // 重新播放一首歌
            try {
                playMp3Url();
            } catch (UnsupportedAudioFileException | IOException e) {
                e.printStackTrace();
            }
        } else {
            // 暂停继续播放
            // 设置播放点
            clip.setFramePosition(point);
            // 继续播放
            clip.start();
        }
    }

}
